package cn.qf.shop.controller;

import cn.qf.shop.pojo.entity.Complaints;
import cn.qf.shop.pojo.entity.Letter;
import cn.qf.shop.pojo.qo.ComplaintsQO;
import cn.qf.shop.pojo.qo.LetterQO;
import cn.qf.shop.pojo.vo.RespResult;
import cn.qf.shop.service.LetterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;
import java.util.List;

@EnableSwagger2
@RequestMapping("/let")
@Api(tags = "信箱管理业务接口")
@RestController
@RestControllerAdvice
public class LetterMainController extends  BaseController {
    private Logger logger = LoggerFactory.getLogger(LetterMainController.class);
    @Resource
    LetterService letterService;
    /**
     * 查询信件服务的所有表格
     */
    @ResponseBody
    @ApiOperation("投诉信件数据展示")
    @PostMapping(value = "/showLet", produces = {"application/json;charset=UTF-8"})
    public RespResult showLet(){
        List<Letter> letters = letterService.showLet();
        RespResult respResult =null;
        if (letters == null) {
            respResult =super.returnRespWithFailed("找不到投诉表单");
        }
        respResult =super.returnRespWithSuccess(letters);
        return  respResult;
    }
    /**
     * 增加一条字段
     *
     * @param letterQO
     * @return
     */
    @ResponseBody
    @ApiOperation("增加信件管理数据")
    @PostMapping(value = "/regLet", produces = {"application/json;charset=UTF-8"})
    public RespResult regLet(@RequestBody LetterQO letterQO) {
        Letter letter = new Letter();
        BeanUtils.copyProperties(letterQO,letter);
        RespResult respResult = new RespResult();
        int i = letterService.addLet(letterQO);
        if (i == 1) {
            respResult = showLet();
        } else {
            respResult = returnRespWithFailed("添加失败");
        }
        return respResult;
    }
    /**
     * 点击编辑
     *
     * @param letterQO
     * @return
     */
    @ResponseBody
    @ApiOperation("修改数据")
    @RequestMapping(value = "/alterLet", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public RespResult alterLet(@RequestBody LetterQO letterQO) {
        Letter letter = new Letter();
        BeanUtils.copyProperties(letterQO,letter);
        RespResult respResult = new RespResult();
        int i = letterService.updateLet(letterQO);
        if (i == 1) {
            respResult = showLet();
        } else {
            respResult = returnRespWithFailed("修改失败");
        }
        return respResult;
    }

    /**
     * 点击删除一条记录
     *
     * @param
     * @return
     */
    @ResponseBody
    @ApiOperation("删除一行")
    @RequestMapping(value = "/deleteLetId", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public RespResult deleteLetId(int letId) {

        RespResult respResult = new RespResult();
        int i = letterService.deleteLet(letId);
        if (i == 1) {
            respResult =showLet();
        } else {
            respResult = returnRespWithFailed("删除失败");
        }
        return respResult;
    }
    /**
     * 点击图标进行全部删除
     *
     * @param letIds
     * @return
     */
    @ResponseBody
    @ApiOperation("批量删除")
    @RequestMapping(value = "/deleteAllLet", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public RespResult deleteAllLet(@RequestParam(value = "letIds[]") List<Integer> letIds) {
        int i = letterService.deleteAllLet(letIds);
        RespResult respResult = null;
        if (i == -1) {
            respResult = super.returnRespWithFailed("删除失败");
        } else {
            respResult = showLet();
        }
        return respResult;
    }
    @ResponseBody
    @ApiOperation("用户模糊查询")
    @RequestMapping(value = "/selectAllLet", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public RespResult selectAllLet(@RequestParam(required = false) String keyword, @RequestParam(required = false) String createTime, @RequestParam(required = false) String updateTime, @RequestParam(defaultValue = "1") int limit, @RequestParam(defaultValue = "10") int offset) {
        RespResult respResult = null;
        List<LetterQO> letterQOS = letterService.letterList(keyword, createTime, updateTime, limit, offset);
        if (letterQOS.size() == 0) {
            respResult = returnRespWithFailed("找不到实体类");
        } else {
            RespResult respResult1 = returnRespWithSuccess(letterQOS);
            return respResult1;
        }
        return respResult;
    }
}
